using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using UnityEditor;
using UnityEditor.VersionControl;
using UnityEngine;
using UnityEngine.UI;

namespace WorldMapTools
{
    
    public class Tools: EditorWindow
    {
        [MenuItem("GTATools/Prefabs/ConvertTreeObjToPrefab")]
        public static void ConvertTreeObjToPrefab()
        {

            var assetPaths = AssetDatabase.GetAllAssetPaths();
            foreach (var path in assetPaths)
            {
                if (path.Contains("Assets/Arts/out/Mesh/") && path.Contains("Grove_M_"))
                {
                    var model = (GameObject)AssetDatabase.LoadAssetAtPath<GameObject>(path);
                    if (model != null)
                    {
                        Renderer[] renderers = model.GetComponentsInChildren<Renderer>();
                        if (renderers == null)
                            return;
                        foreach (var renderer in renderers)
                        {
                            renderer.sharedMaterial = AssetDatabase.LoadAssetAtPath<Material>("Assets/Arts/Material/tree.mat");
                        }

                        var prefab = new GameObject();
                        prefab.gameObject.name = model.name;
                        var mf = prefab.AddComponent<MeshFilter>();
                        mf.sharedMesh = model.GetComponentInChildren<MeshFilter>().sharedMesh;
                        var mr = prefab.AddComponent<MeshRenderer>();
                        mr.sharedMaterial = AssetDatabase.LoadAssetAtPath<Material>("Assets/Arts/Material/tree.mat");
                   
                        PrefabUtility.CreatePrefab("Assets/ResourcesAssets/World/prefabs/trees/"+model.name+".prefab",prefab);
                    }
                }
            }
           
        }

        [MenuItem("GTATools/GamePlay/SandBox/MapTools/PrintMapData")]
        public static void printMapData()
        {
            byte[] b1 = {0x01};
            byte[] b2 = {0x02};
            byte[] b3 = {0x03};
            byte[] b4 = {0x04};
            byte[] b5 = {0x05};
            string s1 = Encoding.ASCII.GetString(b1);
            string s2 = Encoding.ASCII.GetString(b2);
            string s3 = Encoding.ASCII.GetString(b3);
            string s4 = Encoding.ASCII.GetString(b4);
            string s5 = Encoding.ASCII.GetString(b5);
            
            string path = Application.dataPath+"/Arts/mapdata";
            string outpath = Application.dataPath+"/StreamingAssets/convertmap";
            string xx = Application.dataPath;
            string x1x = Application.temporaryCachePath;
            var fpaths = Directory.GetFiles(path, "*");
            foreach (var fpath in fpaths)
            {
                var fname = Path.GetFileName(fpath);
                if (fname.Contains("map_data"))
                {
                    //convertMapDataToTxt(fname);
                    var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/Arts/mapdata/"+fname);
                    if (textAsset != null)
                    {
                        var strings = textAsset.text.Split(s3.ToCharArray());
                        List<string> temp = new List<string>();
                        foreach (var str in strings)
                        {
                            var s = str.Replace(s2, "\n\t\t\t\t## ");
                            s = s.Replace(s1, " $$ ");
                            temp.Add(s);
                        }
                        var content = string.Join("\n", temp);
                        File.WriteAllText(outpath + "/" + fname + "_convert",content);
                    }
                  
                }

                if (fname.Contains("tile_data"))
                {
                    var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/Arts/mapdata/"+fname);
                    if (textAsset != null)
                    {
                        var strings = textAsset.text.Split(s5.ToCharArray());
                        List<string> xx5 = new List<string>();
                        foreach (var str in strings)
                        {
                            var x4 = str.Split(s4.ToCharArray());
                            List<string> xx4 = new List<string>();
                            foreach (var strx in x4)
                            {
                                var x3 =strx.Split(s3.ToCharArray());
                                List<string> temp = new List<string>();
                                foreach (var str3x in x3)
                                {
                                  
                                    
                                        var s = str3x.Replace(s2, "\n\t\t\t\t## ");
                                        s = s.Replace(s1, " $$ ");
                                        temp.Add(s);
                                }
                                xx4.Add(string.Join("\n", temp));
                            }
                            xx5.Add(string.Join("\n xxxxxxxxxxxxxxxxxx4444444444444xxxxxxxxxxxxxxxxxxxxx \n",xx4));
                            
                        }
                        var content = string.Join("\n \n xxxxxxxxxxxxxxxxxxxxxx555555555555555555555xxxxxxxxxxxxxxx \n \n ", xx5);
                        File.WriteAllText(outpath + "/" + fname + "_convert",content);
                    }
                }
            }
        }
    }
}